from datetime import datetime
from typing import Any

from sqlalchemy import Column, DateTime, Integer, String, Boolean, BigInteger, CHAR
from sqlalchemy.ext.declarative import as_declarative, declared_attr


@as_declarative()
class Base:
    id: Any
    __name__: str

    # 生成数据表名
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()


class BaseModel(Base):
    """基础模型类 - 匹配RuoYi数据库结构"""
    __abstract__ = True

    # 使用BigInteger匹配数据库的bigint(20)
    create_dept = Column(BigInteger, comment="创建部门")
    create_by = Column(BigInteger, comment="创建者")
    create_time = Column(DateTime, default=datetime.now, comment="创建时间")
    update_by = Column(BigInteger, comment="更新者")
    update_time = Column(DateTime, onupdate=datetime.now, comment="更新时间")
    remark = Column(String(500), comment="备注")


class TenantMixin:
    """多租户混合类"""
    tenant_id = Column(String(20), default='000000', nullable=False, index=True, comment="租户编号")


class SoftDeleteMixin:
    """软删除混合类"""
    del_flag = Column(CHAR(1), default='0', comment="删除标志（0代表存在 1代表删除）")


class TenantModel(BaseModel, TenantMixin):
    """多租户模型基类"""
    __abstract__ = True


class SoftDeleteModel(BaseModel, SoftDeleteMixin):
    """软删除模型基类"""
    __abstract__ = True